/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xui_lib.component.progress.ratingbar;

import ohos.agp.components.AttrSet;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.miscservices.timeutility.Time;
import ohos.system.OsHelper;

/**
 * AnimationRatingBar
 *
 * @author xuexiang
 * @since 2019-03-26 下午10:50
 */
public class AnimationRatingBar extends RatingBar {
    protected EventHandler handler = null;
    protected Runnable mRunnable;
    protected String mRunnableToken = "";

    /**
     * AnimationRatingBar
     *
     * @param context
     */
    protected AnimationRatingBar(Context context) {
        super(context);
        init();
    }

    /**
     * AnimationRatingBar
     *
     * @param context
     * @param attrs
     */
    protected AnimationRatingBar(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * AnimationRatingBar
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    protected AnimationRatingBar(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        handler = new EventHandler(EventRunner.getMainEventRunner());
        mRunnableToken = OsHelper.getUID() + "";
    }

    /**
     * postRunnable
     *
     * @param runnable
     * @param animationDelay
     */
    protected void postRunnable(Runnable runnable, long animationDelay) {
        long timeMillis = Time.getRealActiveTime() + animationDelay;
        if (mRunnableToken == null) {
            mRunnableToken = OsHelper.getUID() + "";
        }
        if (handler == null) {
            handler = new EventHandler(EventRunner.getMainEventRunner());
        }
        handler.postTimingTask(runnable, mRunnableToken, timeMillis);
    }
}

